package cn.ezios.huo.system.biz.controller;

import cn.ezios.huo.system.dao.domain.SysRole;
import cn.ezios.huo.system.dao.dto.SysRoleDTO;
import cn.ezios.huo.system.dao.vo.SysRoleVO;
import cn.ezios.huo.system.dao.service.SysRoleService;
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sys/role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @PostMapping("")
    public Ret add(@RequestBody SysRoleDTO sysRoleDTO) {
        boolean save = sysRoleService.createRoleWithMenuIds(sysRoleDTO);
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody SysRoleDTO sysRoleDTO) {
        boolean update = sysRoleService.updateRoleWithMenuIds(sysRoleDTO);
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        SysRoleVO sysRole = sysRoleService.getRoleWithLeafMenuIds(id);
        return sysRole != null ? Ret.ok(sysRole) : Ret.error("未找到");
    }

    @GetMapping("page")
    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
                    @RequestParam(value = "size", defaultValue = "10") Integer size, SysRoleDTO dto) {
        IPage<SysRoleVO> page = sysRoleService.queryPage(current, size, dto);
        return Ret.ok(page);
    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
//        boolean remove = sysRoleService.removeById(id);
        boolean remove = sysRoleService.deleteRoleWithMenuIds(id);
        return remove ? Ret.ok() : Ret.error();
    }

    @GetMapping("list-all-roles")
    public Ret listAllRoles() {
        return Ret.ok(sysRoleService.list());
    }
}
